﻿using pluginService;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wpf.Utils
{
    public class IPluginManage
    {
        /// <summary>
        /// 是否启用插件
        /// </summary>
        public ConcurrentDictionary<string,bool> PluginNamesDic { get; set; } = new ConcurrentDictionary<string,bool>();

        //责任链
        public PluginBaseHandle PluginHandle { get; set; } = new DefaultPluginHandle();

        public IPluginManage()
        {
            PluginNamesDic.TryAdd("Default",true);
        }

        public void Add(PluginBaseHandle plugin)
        {
            if(PluginNamesDic.ContainsKey(plugin.Name))
            {
                return;
            }
            if (plugin.Name == "Default")
            {
                return;
            }

            PluginNamesDic.TryAdd(plugin.Name, false);

            PluginBaseHandle lastHandle = PluginHandle;
            while(lastHandle.Next != null)
            {
                lastHandle = lastHandle.Next;
            }
            lastHandle.Next = plugin;
        }
        public void Remove(PluginBaseHandle plugin)
        {

            if (!PluginNamesDic.ContainsKey(plugin.Name))
            {
                return;
            }

            if (plugin.Name == "Default")
            {
                return;
            }
            var curPluginHandle = PluginHandle;
            while(curPluginHandle != null)
            {
                if(curPluginHandle.Next.Name==plugin.Name)
                {
                    curPluginHandle.Next = curPluginHandle.Next.Next; 
                    break;
                }
                curPluginHandle = curPluginHandle.Next;
            }

            PluginNamesDic.Remove(plugin.Name, out var removePluginName);
        }

        /// <summary>
        /// 启用插件
        /// </summary>
        /// <param name="pluginName"></param>
        public void Open(string pluginName)
        {
            if (!PluginNamesDic.ContainsKey(pluginName))
            {
                return;
            }

            if (pluginName == "Default")
            {
                return;
            }
            var curPluginHandle = PluginHandle;
            while (curPluginHandle != null)
            {
                if (curPluginHandle.Next.Name == pluginName)
                {
                    curPluginHandle.Next.Open = true;
                    break;
                }
                curPluginHandle = curPluginHandle.Next;
            }

            PluginNamesDic.TryUpdate(pluginName, true, false);
        }

        public List<string> GetAll()
        {
            return PluginNamesDic.Select(d=>d.Key).ToList();
        }


        public class DefaultPluginHandle : PluginBaseHandle
        {
            public override bool Open => true;
            public override string Name => "Default";

            protected override void Handle(IPluginData t)
            {
                t.Message = "Default " + t.Message;
            }
        }
    }
}
